package com.mall.server.service.pms;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.*;
import com.mall.model.domain.*;
import com.mall.model.dto.PmsProductDto;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.PmsProductVo;
import lombok.Data;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductService {
    @DubboReference
    private PmsProductApi productApi;

    //商品会员价格设置
    @DubboReference
    private PmsMemberPriceApi memberPriceApi;

    //优选专区和商品的关系
    @DubboReference
    private CmsPrefrenceAreaProductRelationApi   prefrenceAreaProductRelationApi;

    //商品参数及自定义规格属性
    @DubboReference
    private PmsProductAttributeValueApi productAttributeValueApi;

    //商品满减价格设置
    @DubboReference
    private PmsProductFullReductionApi productFullReductionApi;

    //商品阶梯价格设置
    @DubboReference
    private PmsProductLadderApi   productLadderApi;

    //商品的sku库存信息
    @DubboReference
    private PmsSkuStockApi skuStockApi;

    //专题和商品关系
    @DubboReference
    private CmsSubjectProductRelationApi subjectProductRelationApi;



    public List<PmsProduct>  simpleList(String keywords) {
        List<PmsProduct>  products=  productApi.simpleList(keywords);
        return products;
    }

    public void updateDeleteStatus(Long[] ids, Integer deleteStatus) {
        List<PmsProduct>  products=  productApi.findByIds(ids);
        if (products.size()>0) {
            for (PmsProduct product : products) {
                if (product!=null){
                    product.setDeleteStatus(deleteStatus);

                    productApi.updateById(product);
                }
            }
        }
    }

    public void updateNewStatus(Long[] ids, Integer newStatus) {
        List<PmsProduct>  products=  productApi.findByIds(ids);
        if (products.size()>0) {
            for (PmsProduct product : products) {
                if (product!=null){
                    product.setNewStatus(newStatus);

                    productApi.updateById(product);
                }
            }
        }
    }

    public void updatePublishStatus(Long[] ids, Integer publishStatus) {
        List<PmsProduct>  products=  productApi.findByIds(ids);
        if (products.size()>0) {
            for (PmsProduct product : products) {
                if (product!=null){
                    product.setPublishStatus(publishStatus);

                    productApi.updateById(product);
                }
            }
        }
    }

    public void updateRecommendStatus(Long[] ids, Integer recommendStatus) {
        List<PmsProduct>  products=  productApi.findByIds(ids);
        if (products.size()>0) {
            for (PmsProduct product : products) {
                if (product!=null){
                    product.setRecommandStatus(recommendStatus);

                    productApi.updateById(product);
                }
            }
        }
    }

    public void updateVerifyStatus(Long[] ids, Integer verifyStatus) {
        List<PmsProduct>  products=  productApi.findByIds(ids);
        if (products.size()>0) {
            for (PmsProduct product : products) {
                if (product!=null){
                    product.setVerifyStatus(verifyStatus);

                    productApi.updateById(product);
                }
            }
        }
    }

    //条件筛选查询
    public CommonPage<PmsProduct> productList(Integer pageNum, Integer pageSize,
                                              Integer brandId, Integer productCategoryId,
                                              Integer publishStatus, Integer verifyStatus,
                                              String keyword, String productSn) {
        Page<PmsProduct> products=productApi.findBySomewhere(pageNum,pageSize,brandId,productCategoryId,publishStatus,
                                                            verifyStatus,keyword,productSn);
        CommonPage<PmsProduct> commonPage = new CommonPage<>(products);
        return commonPage;
    }

    public void saveProduct(PmsProductDto productDto) {
        PmsProduct product = new PmsProduct();
        BeanUtils.copyProperties(productDto,product);
        //保存product
        productApi.save(product);

        //保存该商品的会员价格
        List<PmsMemberPrice> memberPriceList = productDto.getMemberPriceList();
        if (memberPriceList.size() > 0) {
            for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
                if (pmsMemberPrice!=null){
                    pmsMemberPrice.setProductId(product.getId());
                    memberPriceApi.save(pmsMemberPrice);
                }
            }
        }

        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = productDto.getPrefrenceAreaProductRelationList();
        if (prefrenceAreaProductRelationList.size()>0) {
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
                if (cmsPrefrenceAreaProductRelation!=null){
                    cmsPrefrenceAreaProductRelation.setProductId(product.getId());
                    prefrenceAreaProductRelationApi.save(cmsPrefrenceAreaProductRelation);
                }
            }
        }

        List<PmsProductAttributeValue> productAttributeValueList = productDto.getProductAttributeValueList();
        if (productAttributeValueList.size() > 0) {
            for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
                if (pmsProductAttributeValue!=null){
                    pmsProductAttributeValue.setProductId(product.getId());
                    productAttributeValueApi.save(pmsProductAttributeValue);
                }
            }
        }

        List<PmsProductFullReduction> productFullReductionList = productDto.getProductFullReductionList();
        if (productFullReductionList.size() > 0) {
            for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
                if (pmsProductFullReduction!=null){
                    pmsProductFullReduction.setProductId(product.getId());
                    productFullReductionApi.save(pmsProductFullReduction);
                }
            }
        }

        List<PmsProductLadder> productLadderList = productDto.getProductLadderList();
        if (productLadderList.size() > 0) {
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                if (pmsProductLadder!=null){
                    pmsProductLadder.setProductId(product.getId());
                    productLadderApi.save(pmsProductLadder);
                }
            }
        }

        List<PmsSkuStock> skuStockList = productDto.getSkuStockList();
        if (skuStockList.size() > 0) {
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                if (pmsSkuStock!=null){
                    pmsSkuStock.setProductId(product.getId());
                    skuStockApi.save(pmsSkuStock);
                }
            }
        }

        List<CmsSubjectProductRelation> subjectProductRelationList = productDto.getSubjectProductRelationList();
        if (subjectProductRelationList.size() > 0) {
            for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                if (cmsSubjectProductRelation!=null){
                    cmsSubjectProductRelation.setProductId(product.getId());
                    subjectProductRelationApi.save(cmsSubjectProductRelation);
                }
            }
        }

    }


    public void updateProduct(Long id,PmsProductDto productDto) {
        PmsProduct product = new PmsProduct();
        BeanUtils.copyProperties(productDto,product);
        product.setId(id);
        //保存product
        productApi.updateById(product);

        //保存该商品的会员价格
        List<PmsMemberPrice> memberPriceList = productDto.getMemberPriceList();
        if (memberPriceList.size() > 0) {
            for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
                if (pmsMemberPrice!=null){
                    pmsMemberPrice.setProductId(product.getId());
                    memberPriceApi.updateById(pmsMemberPrice);
                }
            }
        }

        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = productDto.getPrefrenceAreaProductRelationList();
        if (prefrenceAreaProductRelationList.size()>0) {
            for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
                if (cmsPrefrenceAreaProductRelation!=null){
                    cmsPrefrenceAreaProductRelation.setProductId(product.getId());
                    prefrenceAreaProductRelationApi.updateById(cmsPrefrenceAreaProductRelation);
                }
            }
        }

        List<PmsProductAttributeValue> productAttributeValueList = productDto.getProductAttributeValueList();
        if (productAttributeValueList.size() > 0) {
            for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
                if (pmsProductAttributeValue!=null){
                    pmsProductAttributeValue.setProductId(product.getId());
                    productAttributeValueApi.updateById(pmsProductAttributeValue);
                }
            }
        }

        List<PmsProductFullReduction> productFullReductionList = productDto.getProductFullReductionList();
        if (productFullReductionList.size() > 0) {
            for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
                if (pmsProductFullReduction!=null){
                    pmsProductFullReduction.setProductId(product.getId());
                    productFullReductionApi.updateById(pmsProductFullReduction);
                }
            }
        }

        List<PmsProductLadder> productLadderList = productDto.getProductLadderList();
        if (productLadderList.size() > 0) {
            for (PmsProductLadder pmsProductLadder : productLadderList) {
                if (pmsProductLadder!=null){
                    pmsProductLadder.setProductId(product.getId());
                    productLadderApi.updateById(pmsProductLadder);
                }
            }
        }

        List<PmsSkuStock> skuStockList = productDto.getSkuStockList();
        if (skuStockList.size() > 0) {
            for (PmsSkuStock pmsSkuStock : skuStockList) {
                if (pmsSkuStock!=null){
                    pmsSkuStock.setProductId(product.getId());
                    skuStockApi.updateById(pmsSkuStock);
                }
            }
        }

        List<CmsSubjectProductRelation> subjectProductRelationList = productDto.getSubjectProductRelationList();
        if (subjectProductRelationList.size() > 0) {
            for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
                if (cmsSubjectProductRelation!=null){
                    cmsSubjectProductRelation.setProductId(product.getId());
                    subjectProductRelationApi.updateById(cmsSubjectProductRelation);
                }
            }
        }
    }

    public PmsProductVo findByIdProductVo(Integer id) {
        //根据id查找product
        PmsProduct product= productApi.findById(id);
        //=============================================
        //根据productID查找PmsMemberPrice
        List<PmsMemberPrice> memberPriceList=memberPriceApi.findByProductId(id);
        //根据productID查找PmsMemberPrice
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList =prefrenceAreaProductRelationApi.findByProductId(id);
        //根据productID查找PmsMemberPrice
        List<PmsProductAttributeValue> productAttributeValueList =productAttributeValueApi.findByProductId(id);
        //根据productID查找PmsMemberPrice
        List<PmsProductFullReduction> productFullReductionList=productFullReductionApi.findByProductId(id);
        //根据productID查找PmsMemberPrice
        List<PmsProductLadder> productLadderList =productLadderApi.findByProductId(id);
        //根据productID查找PmsMemberPrice
        List<PmsSkuStock> skuStockList=skuStockApi.findByProductId(id);
        //根据productID查找PmsMemberPrice
        List<CmsSubjectProductRelation> subjectProductRelationList =subjectProductRelationApi.findByProductId(id);

        //封装成前端需要的Vo对象
        PmsProductVo productVo = PmsProductVo.init(memberPriceList, prefrenceAreaProductRelationList, productAttributeValueList,
                productFullReductionList, productLadderList, skuStockList, subjectProductRelationList, product);

        return productVo;


    }
}
